package com.apps.ftapp.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.MySubjectKeepAdapter;
import com.apps.ftapp.model.SubjectKeepModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;
import es.dmoral.toasty.Toasty;

/**
 * 我的做题记录
 */
public class MySubjectKeepActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    private Gson gson = new Gson();

    private List<SubjectKeepModel.MyTestpaperResultsBean> dataList = new ArrayList();
    private MySubjectKeepAdapter mySubjectKeepAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_subject_keep);
        ButterKnife.bind(this);

        initView();
        qingkong.setVisibility(View.VISIBLE);
        qingkong.setOnClickListener(this);

        setTitle("做题记录");


        loadData();

    }


    private void loadData() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        Async.post("my/record", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    SubjectKeepModel jsonBean = gson.fromJson(new String(responseBody), SubjectKeepModel.class);
                    if (Tools.isIntNull(jsonBean.getSuccess()) == 1) {
                        dataList.addAll(jsonBean.getMyTestpaperResults());
                        mySubjectKeepAdapter.addRes(jsonBean.getMyTestpaperResults());

                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }

    private void initView() {

        mySubjectKeepAdapter = new MySubjectKeepAdapter(getApplicationContext(), dataList);
        listView.setAdapter(mySubjectKeepAdapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap map1 = new HashMap();
                map1.put("id", dataList.get(position).getId());
//                ActivitySkipUtil.skipAnotherActivity(MySubjectKeepActivity.this, StErrorJxActivity.class, map1);
                ActivitySkipUtil.skipAnotherActivity(MySubjectKeepActivity.this, CuotiAllJxActivity.class, map1);
//                ActivitySkipUtil.skipAnotherActivity(MySubjectKeepActivity.this,CuotiJieXiActivity.class,map1);
            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.qingkong:
                qingkong();
                break;
        }
    }

    private void qingkong() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        Async.post("my/record/delete", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    Map jsonBean = gson.fromJson(new String(responseBody), Map.class);
                    if (1 == Tools.isDoubleNull(jsonBean.get("success")) || 1.0 == Tools.isDoubleNull(jsonBean.get("success"))) {
                        Toasty.normal(getApplicationContext(), "清空成功").show();
                        dataList.clear();
                        mySubjectKeepAdapter.notifyDataSetChanged();
                    } else {
                        Toasty.normal(getApplicationContext(), Tools.isNull(jsonBean.get("error"))).show();
                    }

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }
}
